	
var init_datagrid = function(){
	$('#tbl_datagrid').datagrid({
		url : '',
		title:'',
		method: 'post',
		loadMsg: "数据装载中....",
		nowrap: false,
		striped: true,
		pagination: true,
		fitColumns:false,
		rownumbers : true,
		checkbox : false,
		singleSelect : true,
		remoteSort : false,
		toolbar : '#toolbar',
		pageSize : 20,
//			height:autoSetDatagridHeight($('#div_search').height()+20),
		height:$('#div_datagrid').height()-50,
		pageList : [20, 50, 100 ],
		columns : [[  
		{title : 'ID', field : 'id', sortable:true, width : 80,halign:'center',align : 'left',
		  formatter: function(value,row,index){				  
				return value;
			}
		},
		{title : '姓名', field : 'name', sortable:true, width : 180,halign:'center',align : 'left',
		  formatter: function(value,row,index){				  
				return value;
			}
		},
		{title : '状态', field : 'storeStatus', sortable:true, width : 80,halign:'center',align : 'left',
			  formatter: function(value,row,index){				  
					return ({"true":"生效","false":"失效"}[value]) || "";
				}
		}
		]]
	});
	
	//初始化分页工具栏
	var pg = $('#tbl_datagrid').datagrid('getPager');
	$(pg).pagination({
		beforePageText : '第',// 页数文本框前显示的汉字
		afterPageText : '页    共 {pages} 页',
		displayMsg : '当前显示 0 - 0 条记录   共 0 条记录'
	});
};

var search = function(){
	var x_url = basePath + '/admin/getData_datagrid';  
	var param = {};
	$.each($('#form_search').serializeArray(), function(i,data){
		param[data['name']] = data['value'];
	});
	$('#tbl_datagrid').datagrid('options').url = x_url;
	$('#tbl_datagrid').datagrid('load',param);
};

var init_dialog = function(){
	$('#dialog_add').dialog({
	    title: '新增',
	    iconCls:"icon-add",
	    width: 400,
	    height: 200,
	    modal: true,
	    closed:true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
            	add();
            }
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
            	closeAddDialog();
            }
        }]
	});	
	
	$('#dialog_modify').dialog({
	    title: '修改',
	    iconCls:"icon-edit",
	    width: 400,
	    height: 200,
	    modal: true,
	    closed:true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
            	modify();
            }
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
            	closeModifyDialog();
            }
        }]
	});		
};

var showAddDialog = function(){
	$('#dialog_add').dialog('open');
	$('#form_add').form('clear');
};
var add = function(){
	if($('#form_add').form('validate')){
		var x_url = basePath + '/admin/add';  
		var param = {};
		$.each($('#form_add').serializeArray(), function(i,data){
			param[data['name']] = data['value'];
		});
		$.ajax({
			url: x_url,
			type: 'POST',
			dataType: 'json',
			data: param,
		})
		.done(function(result) {
			if (result && result.flag) {
				if (result.flag){
					if (result.data) {
						$.messager.alert('温馨提示','新增成功','info');
					}else{
						$.messager.alert('温馨提示','新增失败','error');
					}				
					closeAddDialog();
					search();
				}else{
					$.messager.alert('温馨提示',result.errorMsg||'新增失败','error');
				}
			};
		})
		.fail(function() {
			$.messager.alert('温馨提示','网络异常','error');
		});
	}
};
var closeAddDialog = function(){
	$('#dialog_add').dialog('close');
};

var showModifyDialog = function(id){
	if(!id){
		var row = $('#tbl_datagrid').datagrid("getSelected");
		id = row && row.id;
	}
	if(!id){
		$.messager.alert('温馨提示','请选中一行数据','warning');
		return ;
	}	
	var x_url = basePath + '/admin/getById';  
	var param = {};
	param['id'] = id ;
	$('#form_modify').form('clear');
	$.ajax({
		url: x_url,
		type: 'GET',
		dataType: 'json',
		data: param,
	})
	.done(function(result) {
		if (result && result.flag && result.data) {
			$('#form_modify').form('load',result.data);
		};
	})
	.fail(function() {
		$.messager.alert('温馨提示','网络异常','error');
	});	
	$('#dialog_modify').dialog('open');
};
var modify = function(){
	if($('#form_modify').form('validate')){
		var x_url = basePath + '/admin/modify';  
		var param = {};
		$.each($('#form_modify').serializeArray(), function(i,data){
			param[data['name']] = data['value'];
		});
		$.ajax({
			url: x_url,
			type: 'POST',
			dataType: 'json',
			data: param,
		})
		.done(function(result) {
			if (result) {
				if (result.flag){
					if (result.data) {
						$.messager.alert('温馨提示','修改成功','info');
					}else{
						$.messager.alert('温馨提示','修改失败','error');
					}				
					closeModifyDialog();
					search();
				}else{
					$.messager.alert('温馨提示',result.errorMsg||'修改失败','error');
				}
			};
		})
		.fail(function() {
			$.messager.alert('温馨提示','网络异常','error');
		});
	}	
};	
var closeModifyDialog = function(){
	$('#dialog_modify').dialog('close');
};

var enable = function(id){
	var row = $('#tbl_datagrid').datagrid("getSelected");
	id = id || (row && row.id);
	if(!id){
		$.messager.alert('温馨提示','请选中一行数据','warning');
		return ;
	}
	if(row.storeStatus){
		$.messager.alert('温馨提示','请选中一行【失效】的数据','warning');
		return ;
	}
	var x_url = basePath + '/admin/enable';  
	var param = {};
	param['id'] = id ;
	$.ajax({
		url: x_url,
		type: 'POST',
		dataType: 'json',
		data: param,
	})
	.done(function(result) {
		if (result) {
			if (result.flag){
				if (result.data) {
					$.messager.alert('温馨提示','启用成功','info');
				}else{
					$.messager.alert('温馨提示','启用失败','error');
				}				
				closeModifyDialog();
				search();
			}else{
				$.messager.alert('温馨提示',result.errorMsg||'启用失败','error');
			}
		};
	})
	.fail(function() {
		$.messager.alert('温馨提示','网络异常','error');
	});	
};
var disable = function(id){
	var row = $('#tbl_datagrid').datagrid("getSelected");
	id = id || (row && row.id);
	if(!id){
		$.messager.alert('温馨提示','请选中一行数据','warning');
		return ;
	}
	if(!row.storeStatus){
		$.messager.alert('温馨提示','请选中一行【生效】的数据','warning');
		return ;
	}
	var x_url = basePath + '/admin/disable';  
	var param = {};
	param['id'] = id ;
	$.ajax({
		url: x_url,
		type: 'POST',
		dataType: 'json',
		data: param,
	})
	.done(function(result) {
		if (result) {
			if (result.flag){
				if (result.data) {
					$.messager.alert('温馨提示','停用成功','info');
				}else{
					$.messager.alert('温馨提示','停用失败','error');
				}				
				closeModifyDialog();
				search();
			}else{
				$.messager.alert('温馨提示',result.errorMsg||'停用失败','error');
			}
		};
	})
	.fail(function() {
		$.messager.alert('温馨提示','网络异常','error');
	});	
};

$(function(){
	init_datagrid();
	init_dialog();
	
	search();	
});